////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include "OpenGL.hpp"

#include <SFML\Graphics.hpp>
#include <SFML\OpenGL.hpp>

namespace smeg {
    namespace tools {
        
////////////////////////////////////////////////////////////
/// Setup the viewport, the projection and modelview matrix
////////////////////////////////////////////////////////////
void SetupWindowProjection( int _iLeft, int _iTop, int _iWidth, int _iHeight, const float* _pfProjectionMatrix ) {
    glViewport(_iLeft, _iTop, _iWidth, _iHeight);

    glMatrixMode( GL_PROJECTION );
    glLoadMatrixf( _pfProjectionMatrix );

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();
}
    
////////////////////////////////////////////////////////////
/// Render a quad with the given blending factor
////////////////////////////////////////////////////////////
void RenderQuad( int _iLeft, int _iTop, int _iRight, int _iBottom, int _iSBlendFactor, int _iDBlendFactor, const sf::Color& _QuadColor ) {
    glColor4ub( _QuadColor.r, _QuadColor.g, _QuadColor.b, _QuadColor.a );
    glBlendFunc( _iSBlendFactor, _iDBlendFactor );

    glBegin( GL_QUADS );
        glVertex2i( _iLeft, _iTop );
        glVertex2i( _iRight, _iTop );
        glVertex2i( _iRight, _iBottom );
        glVertex2i( _iLeft, _iBottom );
    glEnd();
}
    
////////////////////////////////////////////////////////////
/// Render a textured quad with the given blending factor
////////////////////////////////////////////////////////////
void RenderTexturedQuad( int _iLeft, int _iTop, int _iRight, int _iBottom, int _iSBlendFactor, int _iDBlendFactor, const sf::Color& _QuadColor ) {
    glColor4ub( _QuadColor.r, _QuadColor.g, _QuadColor.b, _QuadColor.a );
    glBlendFunc( _iSBlendFactor, _iDBlendFactor );

    glBegin( GL_QUADS );
        glTexCoord2i( 0, 0 ); glVertex2i( _iLeft, _iTop );
        glTexCoord2i( 1, 0 ); glVertex2i( _iRight, _iTop );
        glTexCoord2i( 1, 1 ); glVertex2i( _iRight, _iBottom );
        glTexCoord2i( 0, 1 ); glVertex2i( _iLeft, _iBottom );
    glEnd();
}
    
////////////////////////////////////////////////////////////
/// Render a texture with the given blending factors
////////////////////////////////////////////////////////////
void RenderTexture( const sf::Texture* _pTexture, int _iLeft, int _iTop, int _iRight, int _iBottom, int _iSBlendFactor, int _iDBlendFactor ) {

    sf::Texture::bind( _pTexture );

    RenderTexturedQuad( _iLeft, _iTop, _iRight, _iBottom, _iSBlendFactor, _iDBlendFactor );

    sf::Texture::bind( NULL );
}

////////////////////////////////////////////////////////////
/// Render a shader with the given blending factors
////////////////////////////////////////////////////////////
void RenderShader( const sf::Shader* _pShader, int _iLeft, int _iTop, int _iRight, int _iBottom, int _iSBlendFactor, int _iDBlendFactor ) {

    sf::Shader::bind( _pShader );

    RenderTexturedQuad( _iLeft, _iTop, _iRight, _iBottom, _iSBlendFactor, _iDBlendFactor );

    sf::Shader::bind( NULL );
}

    } // namespace tools
} // namespace smeg